/**
 * 
 */
package org.wangh.csv.gui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.wangh.csv.def.Row;

/**
 * @author wang
 *
 */
public class CSVSheet extends Composite {
	
	private final Table table;
	private final TableViewer tableViewer;

	public CSVSheet(Composite parent, int style) {
		super(parent, style);		
		setLayout(new GridLayout());
		
		table = new Table(this, SWT.FULL_SELECTION);
		tableViewer = new TableViewer(table);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setLabelProvider(new CSVLabelProvider());
		
		redraw();
	}
	
	public void udpate(List<Row> rows){
		
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		Row header = rows.get(0);
		
		List<TableColumn> columnList = new ArrayList<TableColumn>();
		for(int i=0; i<header.getSize(); i++){
			final TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(header.get(i).getIndexName());
			column.setResizable(true);
			column.setWidth(50);
			columnList.add(column);
		}
		
		rows.remove(0);
		tableViewer.setInput(rows);
		redraw();
		
	}

}
